import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
class Node{
    int val;
    Node left;
    Node right;
    public Node(int val){
        this.val=val;
    }
}

public class T3 {

    static int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE,aa,id1,id2;

    public static void main(String[] args) {


        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(),a,b,sum=0;
        int[] ints=new int[n];
        String s;
        HashMap<Integer,Node> hashMap=new HashMap<>();
        Node cur,lr;
        for (int i = 0; i < n; i++) {
            ints[i]=sc.nextInt();
            sum+=ints[i];
        }
        while(sc.hasNext()){
            s=sc.next();
            if(s.equals("ss")) break;
            a=Integer.parseInt(s);
            b=sc.nextInt();
            if(hashMap.containsKey(a)){
                cur=hashMap.get(a);
            }else{
                cur=new Node(a);
                hashMap.put(a, cur);
            }
            if(hashMap.containsKey(b)){
                lr=hashMap.get(b);
            }else{
                lr=new Node(b);
                hashMap.put(b, lr);
            }
            if(cur.left!=null){
                cur.right=lr;
            }else{
                cur.left=lr;
            }
        }

        Node head=hashMap.get(0);
        if (head.left!=null) fun(head.left,ints);
        if (head.right!=null) fun(head.right,ints);
        if(2*max-sum>sum-2*min) {
            System.out.println(id1);
        } else{System.out.println(id2);}

    }
    public static void fun2(int a,int b){
        if (a>max) {
            max=a;
            id1=b;
        }
        if (a<min) {
            min=a;
            id2=b;
        }
    }
    public static int fun(Node node,int[] ints){
        if (node.left==null && node.right==null){
            aa=ints[node.val];
            fun2(aa,node.val);
            return aa;
        }
        if (node.left==null){
            aa=ints[node.val]+fun(node.right,ints);
            fun2(aa,node.val);
            return aa;
        }
        if (node.right==null){
            aa=ints[node.val]+fun(node.left,ints);
            fun2(aa,node.val);
            return aa;
        }else{
            aa=ints[node.val]+fun(node.right,ints)+fun(node.left,ints);
            fun2(aa,node.val);
            return aa;
        }
    }
}
